<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\CommonController;
use App\Http\Controllers\Common\ArrayController;
use App\Model\AdminLog;
use App\Model\HomeLog;
use App\Model\Settings;
use Maatwebsite\Excel\Facades\Excel;

class SystemController extends Controller
{
    /**
     * [adminLogs 加载后台日志页面]
     * @return [object] [执行加载后台日志]
     */
    public function adminLogs()
    {
        $common = new CommonController;
        $Array = new ArrayController;
        $type = $Array->adminLogType;
        $result = AdminLog::select('username', 'type','IP', 'addtime', 'content')->orderBy('id', 'desc')->paginate(5);
     
        return view('/Admin/System/adminLogs', ['result' => $result,'type' => $type]);
    }

    /**
     * [homeLogs 加载前台日志有页面]
     * @return [object] [执行加载前台日志有页面]
     */
    public function homeLogs()
    {
        $common = new CommonController;
        $Array = new ArrayController;
        $type = $Array->adminLogType;
        $result = HomeLog::select('username', 'type','IP', 'addtime', 'content')->orderBy('id', 'desc')->paginate(5);
     
        return view('/Admin/System/homeLogs', ['result' => $result,'type' => $type]);
    }

    /**
     * [settings 加载系统设置页面]
     * @return object [执行加载]
     */
    public function settings()
    {
        $common = new CommonController;
        $result = Settings::first();
        $homeLogo =  $common->downloadFile( $result->home_logo);
        $adminLogo =  $common->downloadFile( $result->admin_logo);
        $pic1 =  $common->downloadFile( $result->pic1);
        $pic2 =  $common->downloadFile( $result->pic2);
        $pic3 =  $common->downloadFile( $result->pic3);
        $pic4 =  $common->downloadFile( $result->pic4);
        $pic5 =  $common->downloadFile( $result->pic5);

        return view('/Admin/System/settings', [
            'result' => $result, 
            'homeLogo' => $homeLogo,
            'adminLogo' => $adminLogo,
            'pic1' => $pic1,
            'pic2' => $pic2,
            'pic3' => $pic3,
            'pic4' => $pic4,
            'pic5' => $pic5,
            ]);
    }

    public function saveSettings(Request $request)
    {
        $common = new CommonController;
        $settings = Settings::first();
        $homeTitle = $request->input('homeTitle');
        $registerCode = $request->input('registerCode');
        $homeLogo = $request->input('homeLogo');

     
        $res = $common->uploadToQiNiu($request, 'homeLogo');
        if ($res) {
            $home_logo = $res['key'];
        } else {
            $home_logo = $settings->home_logo;
        }
        $res = $common->uploadToQiNiu($request, 'adminLogo');
        if ($res) {
             $admin_logo = $res['key'];
        } else {
            $admin_logo = $settings->admin_logo;
        }
        $res = $common->uploadToQiNiu($request, 'pic1');
        if ($res) {
            $pic1 = $res['key'];
        } else {
            $pic1 = $settings->pic1;
        }
        $res = $common->uploadToQiNiu($request, 'pic2');
       if ($res) {
            $pic2 = $res['key'];
        } else {
            $pic2 = $settings->pic2;
        }
        $res = $common->uploadToQiNiu($request, 'pic3');
        if ($res) {
            $pic3 = $res['key'];
        } else {
            $pic3 = $settings->pic3;
        }
        $res = $common->uploadToQiNiu($request, 'pic4');
       if ($res) {
            $pic4 = $res['key'];
        } else {
            $pic4 = $settings->pic4;
        }
        $res = $common->uploadToQiNiu($request, 'pic5');
        if ($res) {
            $pic5 = $res['key'];
        } else {
            $pic5 = $settings->pic5;
        }

        $result = Settings::where('id', 1)->update([
            'home_logo' => $home_logo,
            'admin_logo' => $admin_logo,
            'pic1' => $pic1,
            'pic2' => $pic2,
            'pic3' => $pic3,
            'pic4' => $pic4,
            'pic5' => $pic5,
            'shop_title' => $homeTitle,
            'registerCode' => $registerCode,
            ]);

        if ($result) {
            $common->admin_log(session('adminUser')['username'], 9, "修改系统设置");
            return redirect('/admin/System/settings')->with('content','保存成功');
        } else {        
            return redirect('/admin/System/settings')->with('content','保存失败');
        }
    }

    public function excel()
    {
        $content = AdminLog::select('username','content', 'IP', 'addtime')->orderBy('id', 'desc')->get();
        $result = $content->toArray();
        Excel::create("Excel文档", function($excel) use($result)
        {
            //创建sheet
            $excel->sheet('sheet1',function($sheet)  use($result)
            {
                    //填充每个单元格的内容
                    $sheet->row(1,array('序号','用户名','操作内容','操作时间'));
                    $sheet->setWidth('A', 10);
                    $j = 2;
                    foreach ($result as $k => $v) {
                        $sheet->row($j,array($k+1,$v['username'], $v['content'], $v['IP'],date('Y-m-d H:i:s',$v['addtime'])));
                        $j++;
                    }                    

            });
        })->export('xls');

    }
}
